package cn.hznc.controller.service.icabinet;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.constant.SortRecordEnum;
import cn.hznc.constant.SortRecordReloadEnum;
import cn.hznc.controller.service.toolAnalyze.ToolLifeAnalyzeService;
import cn.hznc.domain.entity.EntryExitRecordEntity;
import cn.hznc.domain.entity.ICabinetRecordEntity;
import cn.hznc.domain.entity.ToolForkInfoEntity;
import cn.hznc.domain.entity.TypeEntity;
import cn.hznc.domain.request.icabinet.*;
import cn.hznc.domain.request.inventory.TooLReturnChartReq;
import cn.hznc.domain.request.order.SortCondition;
import cn.hznc.domain.response.PageResult;
import cn.hznc.mapper.ToolForkInfoMapper;
import cn.hznc.mapper.TypeMapper;
import cn.hznc.repository.IcabinetRepository;
import cn.hznc.repository.TypeRepository;
import cn.hznc.utils.CommonUtil;
import cn.hznc.vo.*;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author：Administrator
 * @Date：2023/11/6 15:25
 */
@Service
public class IcabinetService {

    private static final HashMap<String, String> sortEnumMap = new HashMap<>();

    static {
        sortEnumMap.put("number", "Number");
        sortEnumMap.put("price", "Price");
    }

    @Resource
    private IcabinetRepository icabinetRepository;

    @Resource
    private TypeMapper typeMapper;

    @Resource
    private TypeRepository typeRepository;

    @Resource
    private ToolLifeAnalyzeService toolLifeAnalyzeService;

    @Resource
    private ToolForkInfoMapper toolForkInfoMapper;

//    @Resource
//    private ToolLifeAnalyzeService toolLifeAnalyzeService;

    public Object recordGroupMachineToolIndex(RecordMachineToolIndexReq recordMachineToolIndexReq) {
        //PageHelper.startPage(recordMachineToolIndexReq.getPageNum() , recordMachineToolIndexReq.getPageSize());
        List<RecordGroupMachineToolIndexVo> resultList = icabinetRepository.recordGroupMachineToolIndex(recordMachineToolIndexReq);
        List<RecordGroupMachineToolIndexVo> pageList = new ArrayList<>();
        //PageInfo<RecordGroupMachineToolIndexVo> objectPageInfo = new PageInfo<>();
        SortCondition sortCondition = recordMachineToolIndexReq.getSortCondition();
        if (CollectionUtil.isNotEmpty(resultList) && ObjectUtil.isNotNull(sortCondition)) {
            //排序字段白名单
//            sortConditions = queryProcessReq.getSortCondition().stream().
//                    filter(sortCondition -> sortEnumMap.getOrDefault(sortCondition.getSortColumn(), null) != null).
//                    collect(Collectors.toList());
            //递归排序
            //resultList = recursiveSort(resultList , 0 , sortConditions);
            if (sortEnumMap.containsKey(sortCondition.getSortColumn())) {
                SortRecordEnum sortRecordEnum = SortRecordEnum.valueOf(sortEnumMap.getOrDefault(sortCondition.getSortColumn(), null));
                resultList = sortRecordEnum.sortByCondition(resultList, sortCondition.getSortType());
            }
        }
        pageList = CommonUtil.pageList(resultList, recordMachineToolIndexReq.getPageNum(), recordMachineToolIndexReq.getPageSize());
        return new PageResult<>(pageList, (long) resultList.size());
    }

    public Object recordGroupMachineTool(RecordMachineToolIndexReq recordMachineToolIndexReq) {
        List<RecordGroupMachineToolIndexVo> resultList = icabinetRepository.recordGroupMachineTool(recordMachineToolIndexReq);
        List<RecordGroupMachineToolIndexVo> pageList = new ArrayList<>();
        //PageInfo<RecordGroupMachineToolIndexVo> objectPageInfo = new PageInfo<>();
        SortCondition sortCondition = recordMachineToolIndexReq.getSortCondition();
        if (CollectionUtil.isNotEmpty(resultList) && ObjectUtil.isNotNull(sortCondition)) {
            //排序字段白名单
//            sortConditions = queryProcessReq.getSortCondition().stream().
//                    filter(sortCondition -> sortEnumMap.getOrDefault(sortCondition.getSortColumn(), null) != null).
//                    collect(Collectors.toList());
            //递归排序
            //resultList = recursiveSort(resultList , 0 , sortConditions);
            if (sortEnumMap.containsKey(sortCondition.getSortColumn())) {
                SortRecordEnum sortRecordEnum = SortRecordEnum.valueOf(sortEnumMap.getOrDefault(sortCondition.getSortColumn(), null));
                resultList = sortRecordEnum.sortByCondition(resultList, sortCondition.getSortType());
            }
        }
        pageList = CommonUtil.pageList(resultList, recordMachineToolIndexReq.getPageNum(), recordMachineToolIndexReq.getPageSize());
        return new PageResult<>(pageList, (long) resultList.size());
    }

    public Object searchRecord(SearchRecordReq searchRecordReq) {
        //PageHelper.startPage(searchRecordReq.getPageNum(), searchRecordReq.getPageSize());
        List<ICabinetRecordEntity> icabinetConfigEntities = icabinetRepository.searchRecord(searchRecordReq);
        //PageInfo<ICabinetRecordEntity> pageInfo = new PageInfo<>(icabinetConfigEntities);
        List<ICabinetRecordEntity> pageList = new ArrayList<>();
        //recordEntityList = pageInfo.getList();
        if (ObjectUtil.isNotNull(searchRecordReq.getFlag1()) && searchRecordReq.getFlag1() == 0) {
            icabinetConfigEntities = icabinetConfigEntities.stream().sorted(Comparator.comparing(ICabinetRecordEntity::getSortNumber)).collect(Collectors.toList());
        }
        if (ObjectUtil.isNotNull(searchRecordReq.getFlag1()) && searchRecordReq.getFlag1() == 1) {
            icabinetConfigEntities = icabinetConfigEntities.stream().sorted(Comparator.comparing(ICabinetRecordEntity::getSortNumber).reversed()).collect(Collectors.toList());
        }
        if (ObjectUtil.isNotNull(searchRecordReq.getFlag2()) && searchRecordReq.getFlag2() == 0) {
            icabinetConfigEntities = icabinetConfigEntities.stream().sorted(Comparator.comparing(ICabinetRecordEntity::getSortPrice)).collect(Collectors.toList());
        }
        if (ObjectUtil.isNotNull(searchRecordReq.getFlag2()) && searchRecordReq.getFlag2() == 1) {
            icabinetConfigEntities = icabinetConfigEntities.stream().sorted(Comparator.comparing(ICabinetRecordEntity::getSortPrice).reversed()).collect(Collectors.toList());
        }
        if (ObjectUtil.isNotNull(searchRecordReq.getFlag3()) && searchRecordReq.getFlag3() == 0) {
            icabinetConfigEntities = icabinetConfigEntities.stream().sorted(Comparator.comparing(ICabinetRecordEntity::getTime)).collect(Collectors.toList());
        }
        if (ObjectUtil.isNotNull(searchRecordReq.getFlag3()) && searchRecordReq.getFlag3() == 1) {
            icabinetConfigEntities = icabinetConfigEntities.stream().sorted(Comparator.comparing(ICabinetRecordEntity::getTime).reversed()).collect(Collectors.toList());
        }
        if (CollectionUtil.isNotEmpty(icabinetConfigEntities)) {
            pageList = CommonUtil.pageList(icabinetConfigEntities, searchRecordReq.getPageNum(), searchRecordReq.getPageSize());
        }
        return new PageResult<>(pageList, (long) icabinetConfigEntities.size());
    }

    public Object qryChartByDay(IcabinetChartReq icabinetChartReq) throws ParseException {
        List<String> dateList = CommonUtil.findEveryDayReload(icabinetChartReq.getStartTime(), icabinetChartReq.getEndTime());
        List<ToolTypeAnalysisVo> resultList = new ArrayList<>();
        dateList.forEach(date -> {
            Map<String, String> startAndEndOfOneDay = CommonUtil.findStartAndEndOfOneDay(date);
            ToolTypeAnalysisVo machineToolVo = new ToolTypeAnalysisVo();
            machineToolVo.setDate(date);
            List<ToolTypeAnalysisSimpleVo> machineToolChildList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(icabinetChartReq.getMachineToolNames())) {
                icabinetChartReq.getMachineToolNames().forEach(machineTool -> {
                    ToolTypeAnalysisSimpleVo machineToolSimpleVo = new ToolTypeAnalysisSimpleVo();
                    machineToolSimpleVo.setTypeName(machineTool);
                    machineToolSimpleVo.setNumber(ObjectUtil.isNotNull(icabinetRepository.getNumber(machineTool, startAndEndOfOneDay.get("startTime"), startAndEndOfOneDay.get("endTime"))) ? icabinetRepository.getNumber(machineTool, startAndEndOfOneDay.get("startTime"), startAndEndOfOneDay.get("endTime")) : 0);
                    machineToolSimpleVo.setPrice(icabinetRepository.getPrice(machineTool, startAndEndOfOneDay.get("startTime"), startAndEndOfOneDay.get("endTime")));
                    machineToolChildList.add(machineToolSimpleVo);
                });
            }
            machineToolVo.setList(machineToolChildList);
            resultList.add(machineToolVo);
        });
        return resultList;
    }

    public Object qryMachineToolNameGroup() {
        return icabinetRepository.qryMachineToolNameGroup();
    }

    public Object qryChartByMonth(IcabinetChartReq icabinetChartReq) throws ParseException {
        List<Date> dateList = CommonUtil.findDateList(icabinetChartReq.getStartTime(), icabinetChartReq.getEndTime());
        List<ToolTypeAnalysisVo> resultList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        dateList.forEach(date -> {
            DateStrVo dateStrVo = CommonUtil.findDateListReload(date);
            ToolTypeAnalysisVo machineToolVo = new ToolTypeAnalysisVo();
            machineToolVo.setDate(sdf.format(date));
            List<ToolTypeAnalysisSimpleVo> machineToolChildList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(icabinetChartReq.getMachineToolNames())) {
                icabinetChartReq.getMachineToolNames().forEach(machineTool -> {
                    ToolTypeAnalysisSimpleVo machineToolSimpleVo = new ToolTypeAnalysisSimpleVo();
                    machineToolSimpleVo.setTypeName(machineTool);
                    machineToolSimpleVo.setNumber(ObjectUtil.isNotNull(icabinetRepository.getNumber(machineTool, dateStrVo.getStartTime(), dateStrVo.getEndTime())) ? icabinetRepository.getNumber(machineTool, dateStrVo.getStartTime(), dateStrVo.getEndTime()) : 0);
                    machineToolSimpleVo.setPrice(icabinetRepository.getPrice(machineTool, dateStrVo.getStartTime(), dateStrVo.getEndTime()));
                    machineToolChildList.add(machineToolSimpleVo);
                });
            }
            machineToolVo.setList(machineToolChildList);
            resultList.add(machineToolVo);
        });
        return resultList;
    }


    public Object qryChartByMonthRecord(IcabinetChartReq icabinetChartReq) throws ParseException {
        List<Date> dateList = CommonUtil.findDateList(icabinetChartReq.getStartTime(), icabinetChartReq.getEndTime());
        List<Map<String, Object>> resultList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        if (CollectionUtil.isNotEmpty(icabinetChartReq.getMachineToolNames())) {
            icabinetChartReq.getMachineToolNames().forEach(machineTool -> {
                Map<String, Object> paramMap = new LinkedHashMap<>();
                paramMap.put("type", machineTool);
                dateList.forEach(date -> {
                    DateStrVo dateStrVo = CommonUtil.findDateListReload(date);
                    paramMap.put(sdf.format(date), ObjectUtil.isNotNull(icabinetRepository.getNumber(machineTool, dateStrVo.getStartTime(), dateStrVo.getEndTime())) ? icabinetRepository.getNumber(machineTool, dateStrVo.getStartTime(), dateStrVo.getEndTime()) : 0);
                });
                resultList.add(paramMap);
            });
        }
        return resultList;
    }

    public Object qryChartByMonthRecordPrice(IcabinetChartReq icabinetChartReq) throws ParseException {
        List<Date> dateList = CommonUtil.findDateList(icabinetChartReq.getStartTime(), icabinetChartReq.getEndTime());
        List<Map<String, Object>> resultList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        if (CollectionUtil.isNotEmpty(icabinetChartReq.getMachineToolNames())) {
            icabinetChartReq.getMachineToolNames().forEach(machineTool -> {
                Map<String, Object> paramMap = new LinkedHashMap<>();
                paramMap.put("type", machineTool);
                dateList.forEach(date -> {
                    DateStrVo dateStrVo = CommonUtil.findDateListReload(date);
                    paramMap.put(sdf.format(date), ObjectUtil.isNotNull(icabinetRepository.getPrice(machineTool, dateStrVo.getStartTime(), dateStrVo.getEndTime())) ? icabinetRepository.getPrice(machineTool, dateStrVo.getStartTime(), dateStrVo.getEndTime()) : 0.0);
                });
                resultList.add(paramMap);
            });
        }
        return resultList;
    }

    public Object recordGroupOperatorIndex(RecordGroupOperatorIndexReq recordGroupOperatorIndexReq) {
        List<RecordGroupMachineToolIndexVo> resultList = icabinetRepository.recordGroupOperatorIndex(recordGroupOperatorIndexReq);
        List<RecordGroupMachineToolIndexVo> pageList = new ArrayList<>();
        SortCondition sortCondition = recordGroupOperatorIndexReq.getSortCondition();
        if (CollectionUtil.isNotEmpty(resultList) && ObjectUtil.isNotNull(sortCondition)) {
            if (sortEnumMap.containsKey(sortCondition.getSortColumn())) {
                SortRecordEnum sortRecordEnum = SortRecordEnum.valueOf(sortEnumMap.getOrDefault(sortCondition.getSortColumn(), null));
                resultList = sortRecordEnum.sortByCondition(resultList, sortCondition.getSortType());
            }
        }
        pageList = CommonUtil.pageList(resultList, recordGroupOperatorIndexReq.getPageNum(), recordGroupOperatorIndexReq.getPageSize());
        return new PageResult<>(pageList, (long) resultList.size());
    }

    public Object recordGroupOperator(RecordGroupOperatorIndexReq recordGroupOperatorIndexReq) {
        List<RecordGroupMachineToolIndexVo> resultList = icabinetRepository.recordGroupOperator(recordGroupOperatorIndexReq);
        List<RecordGroupMachineToolIndexVo> pageList = new ArrayList<>();
        SortCondition sortCondition = recordGroupOperatorIndexReq.getSortCondition();
        if (CollectionUtil.isNotEmpty(resultList) && ObjectUtil.isNotNull(sortCondition)) {
            if (sortEnumMap.containsKey(sortCondition.getSortColumn())) {
                SortRecordEnum sortRecordEnum = SortRecordEnum.valueOf(sortEnumMap.getOrDefault(sortCondition.getSortColumn(), null));
                resultList = sortRecordEnum.sortByCondition(resultList, sortCondition.getSortType());
            }
        }
        pageList = CommonUtil.pageList(resultList, recordGroupOperatorIndexReq.getPageNum(), recordGroupOperatorIndexReq.getPageSize());
        return new PageResult<>(pageList, (long) resultList.size());
    }

    public Object queryIntelligentToolChartByDay(IcabinetChartReloadReq icabinetChartReloadReq) throws ParseException {
        List<String> dateList = CommonUtil.findEveryDayReload(icabinetChartReloadReq.getStartTime(), icabinetChartReloadReq.getEndTime());
        List<ToolTypeAnalysisVo> resultList = new ArrayList<>();
        dateList.forEach(date -> {
            Map<String, String> startAndEndOfOneDay = CommonUtil.findStartAndEndOfOneDay(date);
            ToolTypeAnalysisVo machineToolVo = new ToolTypeAnalysisVo();
            machineToolVo.setDate(date);
            List<ToolTypeAnalysisSimpleVo> typeChildList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(icabinetChartReloadReq.getTypeNames())) {
                icabinetChartReloadReq.getTypeNames().forEach(typeId -> {
                    ToolTypeAnalysisSimpleVo typeSimpleVo = new ToolTypeAnalysisSimpleVo();
                    TypeEntity typeEntity = typeMapper.selectById(typeId);
                    if (ObjectUtil.isNull(typeEntity)) {
                        return;
                    }
                    String typeName = typeEntity.getTypeName();
                    typeSimpleVo.setTypeName(typeName);
                    //设置数量
                    List<String> strings = toolLifeAnalyzeService.queryPartnosById(Arrays.asList(typeId));
                    typeSimpleVo.setNumber(ObjectUtil.isNotNull(icabinetRepository.getNumberReload(strings, startAndEndOfOneDay.get("startTime"), startAndEndOfOneDay.get("endTime"), icabinetChartReloadReq.getType())) ? icabinetRepository.getNumberReload(strings, startAndEndOfOneDay.get("startTime"), startAndEndOfOneDay.get("endTime"), icabinetChartReloadReq.getType()) : 0);
                    typeSimpleVo.setPrice(ObjectUtil.isNotNull(icabinetRepository.getPriceReload(strings, startAndEndOfOneDay.get("startTime"), startAndEndOfOneDay.get("endTime"), icabinetChartReloadReq.getType())) ? icabinetRepository.getPriceReload(strings, startAndEndOfOneDay.get("startTime"), startAndEndOfOneDay.get("endTime"), icabinetChartReloadReq.getType()) : 0);
                    typeChildList.add(typeSimpleVo);
                });
            }
            machineToolVo.setList(typeChildList);
            resultList.add(machineToolVo);
        });
        return resultList;
    }

    public Object qryIntelligentToolChartByMonth(IcabinetChartReloadReq icabinetChartReloadReq)  throws ParseException{
        List<Date> dateList = CommonUtil.findDateList(icabinetChartReloadReq.getStartTime(), icabinetChartReloadReq.getEndTime());
        List<ToolTypeAnalysisVo> resultList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        dateList.forEach(date -> {
            DateStrVo dateStrVo = CommonUtil.findDateListReload(date);
            ToolTypeAnalysisVo machineToolVo = new ToolTypeAnalysisVo();
            machineToolVo.setDate(sdf.format(date));
            List<ToolTypeAnalysisSimpleVo> machineToolChildList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(icabinetChartReloadReq.getTypeNames())) {
                icabinetChartReloadReq.getTypeNames().forEach(typeId -> {
                    ToolTypeAnalysisSimpleVo typeSimpleVo = new ToolTypeAnalysisSimpleVo();
                    TypeEntity typeEntity = typeMapper.selectById(typeId);
                    if (ObjectUtil.isNull(typeEntity)) {
                        return;
                    }
                    String typeName = typeEntity.getTypeName();
                    typeSimpleVo.setTypeName(typeName);
                    //设置数量
                    List<String> strings = toolLifeAnalyzeService.queryPartnosById(Arrays.asList(typeId));
                    typeSimpleVo.setNumber(ObjectUtil.isNotNull(icabinetRepository.getNumberReload(strings, dateStrVo.getStartTime(), dateStrVo.getEndTime(), icabinetChartReloadReq.getType())) ? icabinetRepository.getNumberReload(strings, dateStrVo.getStartTime(), dateStrVo.getEndTime(), icabinetChartReloadReq.getType()) : 0);
                    typeSimpleVo.setPrice(ObjectUtil.isNotNull(icabinetRepository.getPriceReload(strings, dateStrVo.getStartTime(), dateStrVo.getEndTime(), icabinetChartReloadReq.getType())) ? icabinetRepository.getPriceReload(strings, dateStrVo.getStartTime(), dateStrVo.getEndTime(), icabinetChartReloadReq.getType()) : 0);
                    machineToolChildList.add(typeSimpleVo);
                });
            }
            machineToolVo.setList(machineToolChildList);
            resultList.add(machineToolVo);
        });
        return resultList;
    }

    public Object qryChartByMonthRecordNumberReload(IcabinetChartReloadReq icabinetChartReq) throws ParseException{
        List<Date> dateList = CommonUtil.findDateList(icabinetChartReq.getStartTime(), icabinetChartReq.getEndTime());
        List<Map<String, Object>> resultList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        if (CollectionUtil.isNotEmpty(icabinetChartReq.getTypeNames())) {
            icabinetChartReq.getTypeNames().forEach(typeId -> {
                Map<String, Object> paramMap = new LinkedHashMap<>();
                dateList.forEach(date -> {
                    TypeEntity typeEntity = typeMapper.selectById(typeId);
                    if (ObjectUtil.isNull(typeEntity)) {
                        return;
                    }
                    String typeName = typeEntity.getTypeName();
                    paramMap.put("type", typeName);
                    //设置数量
                    List<String> strings = toolLifeAnalyzeService.queryPartnosById(Arrays.asList(typeId));
                    DateStrVo dateStrVo = CommonUtil.findDateListReload(date);
                    paramMap.put(sdf.format(date), ObjectUtil.isNotNull(icabinetRepository.getNumberReload(strings, dateStrVo.getStartTime(), dateStrVo.getEndTime(), icabinetChartReq.getType())) ? icabinetRepository.getNumberReload(strings, dateStrVo.getStartTime(), dateStrVo.getEndTime(), icabinetChartReq.getType()) : 0);
                });
                resultList.add(paramMap);
            });
        }
        return resultList;
    }

    public Object qryChartByMonthRecordPriceReload(IcabinetChartReloadReq icabinetChartReloadReq) throws ParseException{
        List<Date> dateList = CommonUtil.findDateList(icabinetChartReloadReq.getStartTime(), icabinetChartReloadReq.getEndTime());
        List<Map<String, Object>> resultList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        if (CollectionUtil.isNotEmpty(icabinetChartReloadReq.getTypeNames())) {
            icabinetChartReloadReq.getTypeNames().forEach(typeId -> {
                Map<String, Object> paramMap = new LinkedHashMap<>();
                dateList.forEach(date -> {
                    TypeEntity typeEntity = typeMapper.selectById(typeId);
                    if (ObjectUtil.isNull(typeEntity)) {
                        return;
                    }
                    String typeName = typeEntity.getTypeName();
                    paramMap.put("type", typeName);
                    //设置数量
                    List<String> strings = toolLifeAnalyzeService.queryPartnosById(Arrays.asList(typeId));
                    DateStrVo dateStrVo = CommonUtil.findDateListReload(date);
                    paramMap.put(sdf.format(date), ObjectUtil.isNotNull(icabinetRepository.getPriceReload(strings, dateStrVo.getStartTime(), dateStrVo.getEndTime(), icabinetChartReloadReq.getType())) ? icabinetRepository.getPriceReload(strings, dateStrVo.getStartTime(), dateStrVo.getEndTime(), icabinetChartReloadReq.getType()) : 0);
                });
                resultList.add(paramMap);
            });
        }
        return resultList;
    }

    public Object recordGroupCraftIndex(RecordGroupCraftIndexReq recordGroupCraftIndexReq) {
        List<RecordGroupCraftToolIndexVo> resultList = icabinetRepository.recordGroupCraftIndex(recordGroupCraftIndexReq);
        List<RecordGroupCraftToolIndexVo> pageList = new ArrayList<>();
        SortCondition sortCondition = recordGroupCraftIndexReq.getSortCondition();
        if (CollectionUtil.isNotEmpty(resultList) && ObjectUtil.isNotNull(sortCondition)) {
            if (sortEnumMap.containsKey(sortCondition.getSortColumn())) {
                SortRecordReloadEnum sortRecordEnum = SortRecordReloadEnum.valueOf(sortEnumMap.getOrDefault(sortCondition.getSortColumn(), null));
                resultList = sortRecordEnum.sortByConditionReload(resultList, sortCondition.getSortType());
            }
        }
        pageList = CommonUtil.pageList(resultList, recordGroupCraftIndexReq.getPageNum(), recordGroupCraftIndexReq.getPageSize());
        return new PageResult<>(pageList, (long) resultList.size());
    }

    private RecordGroupCraftToolIndexVo transCraftIndexVo(RecordGroupMachineToolIndexVo recordGroupMachineToolIndexVo) {
        RecordGroupCraftToolIndexVo craftToolIndexVo = new RecordGroupCraftToolIndexVo();
        BeanUtil.copyProperties(recordGroupMachineToolIndexVo , craftToolIndexVo);

        return null;
    }

    public Object recordGroupCraft(RecordGroupCraftIndexReq recordGroupCraftIndexReq) {
        List<RecordGroupCraftToolIndexVo> resultList = icabinetRepository.recordGroupCraft(recordGroupCraftIndexReq);
        //List<RecordGroupCraftToolIndexVo> resultList = icabinetRepository.recordGroupCraftIndex(recordGroupCraftIndexReq);
        List<RecordGroupCraftToolIndexVo> pageList = new ArrayList<>();
        SortCondition sortCondition = recordGroupCraftIndexReq.getSortCondition();
        if (CollectionUtil.isNotEmpty(resultList) && ObjectUtil.isNotNull(sortCondition)) {
            if (sortEnumMap.containsKey(sortCondition.getSortColumn())) {
                SortRecordReloadEnum sortRecordEnum = SortRecordReloadEnum.valueOf(sortEnumMap.getOrDefault(sortCondition.getSortColumn(), null));
                resultList = sortRecordEnum.sortByConditionReload(resultList, sortCondition.getSortType());
            }
        }
        pageList = CommonUtil.pageList(resultList, recordGroupCraftIndexReq.getPageNum(), recordGroupCraftIndexReq.getPageSize());
        return new PageResult<>(pageList, (long) resultList.size());
    }

    public Object icabinetDeadStock( IcabinetDeadStockReq icabinetDeadStockReq) {
        List<String> partnos = icabinetDeadStockReq.getPartnos();
        List<IcabinetDeadStockVo> icabinetDeadStockVos = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(partnos)){
            partnos = CommonUtil.pageList(partnos , icabinetDeadStockReq.getPageNum() , icabinetDeadStockReq.getPageSize());
            partnos.forEach(s -> {
                IcabinetDeadStockVo icabinetDeadStockVo = new IcabinetDeadStockVo();
                icabinetDeadStockVo.setPartno(s);
                QueryWrapper<ToolForkInfoEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("partno" , s);
                List<ToolForkInfoEntity> toolForkInfoEntities = toolForkInfoMapper.selectList(wrapper);
                if(CollectionUtil.isNotEmpty(toolForkInfoEntities)){
                    ToolForkInfoEntity toolForkInfoEntity = toolForkInfoEntities.get(0);
                    icabinetDeadStockVo.setIdnr(toolForkInfoEntity.getIdnr());
                    icabinetDeadStockVo.setStockNumber(icabinetRepository.countNumber(toolForkInfoEntity.getId()));
                    if(ObjectUtil.isNotNull(toolForkInfoEntity.getPrice())){
                        icabinetDeadStockVo.setPrice(toolForkInfoEntity.getPrice());
                        icabinetDeadStockVo.setTotalPrice(icabinetDeadStockVo.getPrice());
                    }
                }
                if(ObjectUtil.isNotNull(icabinetDeadStockReq.getStartTime())){
                    Double unitPrice = icabinetRepository.getPriceByPartno(s);
                    icabinetDeadStockVo.setInboundPrice(unitPrice * icabinetRepository.getInboundPrice(icabinetDeadStockReq.getStartTime() , icabinetDeadStockReq.getEndTime() , s));
                    icabinetDeadStockVo.setOutBoundPrice(unitPrice * icabinetRepository.getOutboundPrice(icabinetDeadStockReq.getStartTime() , icabinetDeadStockReq.getEndTime() , s));
                }
                icabinetDeadStockVos.add(icabinetDeadStockVo);
            });
        }
        return icabinetDeadStockVos;
    }

    public Object queryToolReturnChartByDay(TooLReturnChartReq tooLReturnChartReq) throws ParseException {
        List<String> dateList = CommonUtil.findEveryDayReload(tooLReturnChartReq.getStartTime(), tooLReturnChartReq.getEndTime());
        List<ToolTypeAnalysisVo> resultList = new ArrayList<>();
        dateList.forEach(date -> {
            Map<String, String> startAndEndOfOneDay = CommonUtil.findStartAndEndOfOneDay(date);
            ToolTypeAnalysisVo machineToolVo = new ToolTypeAnalysisVo();
            machineToolVo.setDate(date);
            List<ToolTypeAnalysisSimpleVo> typeChildList = new ArrayList<>();
            if (ObjectUtil.isNotNull(tooLReturnChartReq.getParentId())) {
                List<TypeEntity> typeNames =  typeRepository.selectByIds(tooLReturnChartReq.getParentId());
                if(CollectionUtil.isNotEmpty(typeNames)){
                    typeNames.forEach(typeEntity ->{
                        ToolTypeAnalysisSimpleVo typeSimpleVo = new ToolTypeAnalysisSimpleVo();
                        typeSimpleVo.setTypeName(typeEntity.getTypeName());
                        //设置数量
                        List<String> strings = toolLifeAnalyzeService.queryPartnosById(Arrays.asList(typeEntity.getId()));
                        //计算零件的总数
                        typeSimpleVo.setNumber(ObjectUtil.isNotNull(icabinetRepository.countNumberReload(strings, startAndEndOfOneDay.get("startTime"), startAndEndOfOneDay.get("endTime"))) ? icabinetRepository.countNumberReload(strings, startAndEndOfOneDay.get("startTime"), startAndEndOfOneDay.get("endTime")) : 0);
                        //计算总成的总数
                        List<EntryExitRecordEntity> recordEntities = icabinetRepository.selectByCondition(startAndEndOfOneDay.get("startTime"), startAndEndOfOneDay.get("endTime"));
                        Integer partNum = buildAssemblePartNum(recordEntities , strings);
                        if(ObjectUtil.isNotNull(partNum) && partNum>0){
                            typeSimpleVo.setNumber(typeSimpleVo.getNumber());
                        }
                        typeChildList.add(typeSimpleVo);
                    });
                }
                machineToolVo.setList(typeChildList);
                resultList.add(machineToolVo);
            }
        });
        return resultList;
    }

    private Integer buildAssemblePartNum(List<EntryExitRecordEntity> recordEntities , List<String> partnos) {
        Map<String , Integer> partNumMap = new HashMap<>();
        Integer num = 0;
        if(CollectionUtil.isNotEmpty(recordEntities)){
//            recordEntities.stream().filter(entryExitRecordEntity -> StringUtils.isNotBlank(entryExitRecordEntity.getToolParams())).map(entryExitRecordEntity -> )
            for (EntryExitRecordEntity entryExitRecordEntity : recordEntities) {
                if(StringUtils.isNotBlank(entryExitRecordEntity.getToolParams())){
                    List<ToolForkInfoCountVo> countVos = JSONArray.parseArray(entryExitRecordEntity.getToolParams() , ToolForkInfoCountVo.class);
                    countVos = countVos
                            .stream()
                            .filter(toolForkInfoCountVo -> StringUtils.isNotBlank(toolForkInfoCountVo.getPartno())&&partnos.contains(toolForkInfoCountVo.getPartno()))
                            .collect(Collectors.toList());
                    if(CollectionUtil.isNotEmpty(countVos)){
                        num += countVos.stream().mapToInt(ToolForkInfoCountVo::getNumber).sum();
                    }
                }
            }
        }
        return num;
    }

//    public Object queryTypeNames() {
//        QueryWrapper<TypeEntity> wrapper = new QueryWrapper<>();
//        wrapper.eq("parent_id" , -1);
//        TypeEntity typeEntity = typeMapper.selectOne(wrapper);
//        if(ObjectUtil.isNotNull(typeEntity)){
//
//        }
//        return null;
//    }
}